/*
 *  OTSComponent.cpp
 *
 *  Copyright (C) 2005 Christopher Taylor <christopher.paul.taylor@gmail.com>
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
 #include <stdio.h>

#include "OTSComponent.hpp"
#include "nsMemory.h"
#include "nsIGenericFactory.h"

/*
    libots is written in pure C we need to fix the decoration of the header file by the parser
    this is the implementation file for IOTSComponent.idl
*/

#ifndef __cplusplus
extern "C" {
	#include "libots.h"
}
#else
	#include "libots.h"
#endif
	
/* Implementation file */
NS_IMPL_ISUPPORTS1(OTSComponent, IOTSComponent)

OTSComponent::OTSComponent()
{
  /* member initializers and constructor code */
}

OTSComponent::~OTSComponent()
{
  /* destructor code */
}

/* 
    return a text summary to the caller

    Summary(const char *dictionary, const char *text, PRBool highlight, PRInt32 percentage, char **_retval);
    
    const char* dictionary:  xml dictionary file to pass to OTS
    const char* text: the input text
    PRBool highlight: boolean value to highlight in the text the summary
    PRInt32 percentage: how much of the document to summarize; implied ceiling value of 100%
    char **_retval: a character buffer that was created in Javascript - this is the return value for the method
*/

NS_IMETHODIMP OTSComponent::Summary(const char *dictionary, const char *text, PRBool highlight, PRInt32 percentage, char **_retval)
{
   
	char *result = 0;
   
    //
	// check to make sure the return value in the "Javascript world" has memory allocated to it
    //
	if(!_retval)
    {
		return NS_ERROR_NULL_POINTER;
    }
	
	OtsArticle *Art = ots_new_article();
  
	if(!ots_load_xml_dictionary(Art, dictionary))
	{
		
		printf("Error processing dictionary\t%s\n", dictionary);
		fflush(stdout);
		
		ots_free_article(Art);
		result = "<html><head><title>OTS Summary</title></head><body>Couldn't load OTS Dictionary!</body></html>\n";
		*_retval = (char *)nsMemory::Clone(result, sizeof(char)*(strlen(result)+1));
		return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
	}

	size_t size = strlen((char *)text);
	ots_parse_stream((unsigned char *) text, size, Art);
	ots_grade_doc(Art);
	ots_highlight_doc(Art, (int)percentage);
	
    // highlight ots selected text in the document - original text is included
	if(highlight == PR_TRUE)
    {
		result = (char *)ots_get_doc_HTML(Art, &size);
    }
    // capture only the summary text
	else
    {
		result = (char *)ots_get_doc_text(Art, &size);
    }
	
	// alloc memory for the return value so that XPCOM can control the life time of this string.
	*_retval = (char *)nsMemory::Clone(result, sizeof(char)*(strlen(result)+1));
	
	ots_free_article(Art);

	return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

/* End of implementation class template. */

